//
//  loginViewController.m
//  BestPurchase
//
//  Created by ZSMAC on 2018/10/10.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "loginViewController.h"
#import "RegisteredViewController.h"
@interface loginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *registeredBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgotPasswordBtn;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;
@end

@implementation loginViewController
- (void)viewWillAppear:(BOOL)animated {
    if (!self.navigationController.navigationBarHidden) {
        self.navigationController.navigationBarHidden=YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self UIConfiguer];
}
- (void)UIConfiguer {
    self.accountTextField.clearButtonMode=UITextFieldViewModeAlways;
    self.passwordField.clearButtonMode=UITextFieldViewModeAlways;
    self.passwordField.secureTextEntry=YES;
    self.accountTextField.keyboardType=UIKeyboardTypeNumberPad;

    
    self.loginBtn.layer.cornerRadius=5;
    self.loginBtn.layer.masksToBounds=YES;
}
#pragma mark 登录
- (IBAction)loginBtnCilcked:(id)sender {
    [self View_TouchDown];
    
    NSString *account  =self.accountTextField.text;
    if (![NSString isValidateMobile:account]) {
        [ZSProgressHUD showDpromptText:@"请输入正确的手机号"];
        return;
    }
    if (self.passwordField.text.length==0 || [self.passwordField.text isEqualToString:@""]) {
        [ZSProgressHUD showDpromptText:@"请输入密码"];
        return;
    }
    
    self.view.userInteractionEnabled=NO;
    [ZSProgressHUD showHUDShowText:@"加载中..."];
    NSString * accountStr =self.accountTextField.text;
    NSString * passwordStr =self.passwordField.text;
    @WeakObj(self);
    if (accountStr.length!=0 && passwordStr.length!=0) {
        [BmobUser loginInbackgroundWithAccount:accountStr andPassword:passwordStr block:^(BmobUser *user, NSError *error) {
            if (error) {
                 NSLog(@"登录失败 %@",error);
                [ZSProgressHUD showDpromptText:@"账号或密码错误"];
                selfWeak.view.userInteractionEnabled=YES;
            } else {
                 NSLog(@"登录成功 %@",user);
                [ZSProgressHUD showSuccessfulAnimatedText:@"登录成功!"];
                selfWeak.view.userInteractionEnabled=YES;
                [selfWeak performSelector:@selector(backClicked:) withObject:nil afterDelay:1.5];
            }
        }];
    }
}
#pragma mark 注册
- (IBAction)registeredBtnClicked:(id)sender {
    RegisteredViewController * regisereVC = [[RegisteredViewController alloc] init];
    [self.navigationController pushViewController:regisereVC animated:YES];
 }
#pragma mark 忘记密码
- (IBAction)forgotPasswordCilcked:(id)sender {
    RegisteredViewController * regisereVC = [[RegisteredViewController alloc] init];
    regisereVC.isRegistered=NO;
    [self.navigationController pushViewController:regisereVC animated:YES];
}
#pragma mark 返回
- (IBAction)backClicked:(id)sender {
     [ZSProgressHUD hideAllHUDAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
